home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-10-16 | 3.1 KB | 152 lines | [TEXT/CWIE] |
- import java.awt.*;
-
- public class shapeCanvas extends Canvas
- {
- final int rectangle=0,
- rect3D=1,
- roundRect=2,
- arc=3,
- oval=4,
- line=5,
- polygon=6;
- int whichShape = rectangle;
- boolean isFilled = true;
-
- shapeCanvas( int width, int height )
- {
- setBackground( Color.yellow );
- setForeground( Color.red );
- resize( width, height );
- }
-
- public void SetShape( int newShape )
- {
- whichShape = newShape;
- }
-
- public void SetIsFilled( boolean newIsFilled )
- {
- isFilled = newIsFilled;
- }
-
- public void paint( Graphics g )
- {
- Rectangle r, b;
- Polygon poly;
-
- b = bounds();
- r = new Rectangle( 10, 10, b.width - 20, b.height - 20 );
-
- poly = new Polygon();
- poly.addPoint( r.x, r.y );
- poly.addPoint( r.x, r.y + r.height );
- poly.addPoint( r.x + r.width, r.y + r.height );
- poly.addPoint( r.x, r.y + r.height/2 );
- poly.addPoint( r.x + r.width, r.y + r.height/2 );
- poly.addPoint( r.x, r.y );
-
- if ( isFilled )
- {
- switch ( whichShape )
- {
- case rectangle:
- g.fillRect( r.x, r.y, r.width, r.height );
- break;
- case rect3D:
- g.fill3DRect( r.x, r.y, r.width, r.height, true );
- break;
- case roundRect:
- g.fillRoundRect( r.x, r.y, r.width, r.height, 16, 16 );
- break;
- case arc:
- g.fillArc( r.x, r.y, r.width, r.height, 0, 270 );
- break;
- case oval:
- g.fillOval( r.x, r.y, r.width, r.height );
- break;
- case line:
- g.drawLine( r.x, r.y, r.x + r.width, r.y + r.height );
- break;
- case polygon:
- g.fillPolygon( poly );
- break;
- }
- }
- else
- {
- switch ( whichShape )
- {
- case rectangle:
- g.drawRect( r.x, r.y, r.width, r.height );
- break;
- case rect3D:
- g.draw3DRect( r.x, r.y, r.width, r.height, true );
- break;
- case roundRect:
- g.drawRoundRect( r.x, r.y, r.width, r.height, 16, 16 );
- break;
- case arc:
- g.drawArc( r.x, r.y, r.width, r.height, 0, 270 );
- break;
- case oval:
- g.drawOval( r.x, r.y, r.width, r.height );
- break;
- case line:
- g.drawLine( r.x, r.y, r.x + r.width, r.y + r.height );
- break;
- case polygon:
- g.drawPolygon( poly );
- break;
- }
- }
- }
- }
-
- public class javaDraw extends java.applet.Applet
- {
- private Choice shapePopup, fillPopup;
- private shapeCanvas sCanvas;
-
- public void init()
- {
- shapePopup = new Choice();
- shapePopup.addItem( "Rectangle" );
- shapePopup.addItem( "3D Rectangle" );
- shapePopup.addItem( "Rounded Rectangle" );
- shapePopup.addItem( "Arc" );
- shapePopup.addItem( "Oval" );
- shapePopup.addItem( "Line" );
- shapePopup.addItem( "Polygon" );
-
- add( shapePopup );
-
- fillPopup = new Choice();
- fillPopup.addItem( "Fill Shapes" );
- fillPopup.addItem( "Do Not Fill Shapes" );
-
- add( fillPopup );
-
- sCanvas = new shapeCanvas( 208, 110 );
- add( sCanvas );
- }
-
- public boolean action( Event e, Object obj )
- {
- if ( e.target == shapePopup )
- {
- sCanvas.SetShape( shapePopup.getSelectedIndex() );
- sCanvas.repaint();
-
- return true;
- }
- else if ( e.target == fillPopup )
- {
- sCanvas.SetIsFilled( fillPopup.getSelectedIndex() == 0 );
- sCanvas.repaint();
-
- return true;
- }
- else
- return super.action( e, obj );
- }
- }